/**
 * @Title: SmsServiceImpl.java
 * @Package com.leesche.zcsp.node.communication.service.impl
 * @Description:()
 * @author dennies yang
 * @date 2016年10月18日 下午6:20:01
 * @version V1.0
 */
package com.leesche.zcsp.node.communication.service.impl;

import com.alibaba.fastjson.JSON;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.profile.IClientProfile;
import com.aliyuncs.sms.model.v20160927.SingleSendSmsRequest;
import com.aliyuncs.sms.model.v20160927.SingleSendSmsResponse;
import com.leesche.zcsp.node.communication.constants.CncContants;
import com.leesche.zcsp.node.communication.service.SmsService;
import com.leesche.zcsp.node.pay.wecha.data.Message;
import com.leesche.zcsp.node.utils.NetConfig;
import com.leesche.zcsp.node.utils.web.messge.WebResult;
import com.leesche.zcsp.node.web.constants.Constants;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author dennies yang
 * @version V1.0
 * @Title: SmsServiceImpl.java
 * @Package com.leesche.zcsp.node.communication.service.impl
 * @Description:()
 * @date 2016年10月18日 下午6:20:01
 */
@Service
public class SmsServiceImpl implements SmsService {
    private final static Logger logger = Logger.getLogger(SmsServiceImpl.class);

    @Override
    public Message<String> sendSMS(String mobile, String content) {
        try {
            IClientProfile profile = DefaultProfile.getProfile("cn-hangzhou", NetConfig.accesskey,
                    NetConfig.accessSecret);
            DefaultProfile.addEndpoint("cn-hangzhou", "cn-hangzhou", "Sms", "sms.aliyuncs.com");
            IAcsClient client = new DefaultAcsClient(profile);
            SingleSendSmsRequest request = new SingleSendSmsRequest();
            request.setSignName(CncContants.ALITYU_SMSSIGN);
            request.setTemplateCode(CncContants.ALITYU_SMSTEMPLE);
            request.setParamString("{\"code\":\"" + content + "\"}");
            request.setRecNum(mobile);
            SingleSendSmsResponse httpResponse = client.getAcsResponse(request);
            logger.info("请求" + httpResponse.getRequestId());
            httpResponse.getRequestId();

        } catch (ServerException e) {
            logger.error(e.getMessage(),e);
            return Message.error(e.getErrCode(), null);
        } catch (ClientException e) {
            logger.error(e.getMessage(),e);
            return Message.error(e.getErrCode(), null);
        }
        return Message.success("发送成功", null);
    }

    @Override
    public Message<String> sendSMS(String mobile, Map<String, String> contents, String templateCode) {
        try {
            IClientProfile profile = DefaultProfile.getProfile("cn-hangzhou", NetConfig.accesskey,
                    NetConfig.accessSecret);
            DefaultProfile.addEndpoint("cn-hangzhou", "cn-hangzhou", "Sms", "sms.aliyuncs.com");
            IAcsClient client = new DefaultAcsClient(profile);
            SingleSendSmsRequest request = new SingleSendSmsRequest();
            request.setSignName(CncContants.ALITYU_SMSSIGN);
            request.setTemplateCode(templateCode);
            request.setParamString(JSON.toJSONString(contents));
            request.setRecNum(mobile);
            SingleSendSmsResponse httpResponse = client.getAcsResponse(request);
            logger.info("请求" + httpResponse.getRequestId());
            httpResponse.getRequestId();

        } catch (ServerException e) {
            logger.error(e.getMessage(),e);
            return Message.error(e.getErrCode(), null);
        } catch (ClientException e) {
            logger.error(e.getMessage(),e);
            return Message.error(e.getErrCode(), null);
        } catch (Exception e) {
            logger.error(e.getMessage(),e);
            return Message.error(e.getMessage(), null);
        }
        return Message.success("发送成功", null);
    }

    /**
     * @param mobile 用户手机号
     * @param name   用户姓名
     * @Title: giveInfoAcceptSMS(String token)
     * @Description: TODO(给用户发送进件受理通知)
     * @author Jerry
     * @date 2016-10-29 04:26:57
     */
    @Override
    public void giveInfoAcceptSMS(String mobile, String name) {
        try {
            Map<String, String> contents = new HashMap<String, String>();
            contents.put(CncContants.KEY_CONTENT_NAME, name);
            contents.put(CncContants.KEY_CONTENT_MOBILE, mobile);
            sendSMS(mobile, contents, CncContants.ALITYU_SMSTEMPLE_USERGIVEINFO_ACCEPT);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
    }

    /**
     * @param name    用户姓名
     * @param mobiles 审核员接收短信的号码组
     * @Title: giveInfoNotifySMS(String[] mobiles, String name)
     * @Description: TODO(给审核员发送客户进件通知)
     * @author Jerry
     * @date 2016-10-29 04:26:57
     */
    @Override
    public void giveInfoNotifySMS(List<String> mobiles, String name) {
        try {
            Map<String, String> contents = new HashMap<String, String>();
            contents.put(CncContants.KEY_CONTENT_NAME, name);
            for (String mobile : mobiles) {
                sendSMS(mobile, contents, CncContants.ALITYU_SMSTEMPLE_USERGIVEINFO_ADMIN);
            }
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
    }

    /**
     * @param mobile 用户手机号
     * @param name   用户姓名
     * @param result 审核结果
     * @Title: giveInfoAcceptSMS(String token)
     * @Description: TODO(给用户发送进件审核结果通知)
     * @author Jerry
     * @date 2016-10-29 04:26:57
     */
    public void giveInfoVerifyResultSMS(String mobile, String name, String result) {
        try {
            Map<String, String> contents = new HashMap<String, String>();
            contents.put(CncContants.KEY_CONTENT_NAME, name);
            contents.put(CncContants.KEY_CONTENT_RESULT, result);
            Message<String> re = sendSMS(mobile, contents, CncContants.ALITYU_SMSTEMPLE_USERGIVEINFO_RESULT);
            System.out.println(re.getContent());
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
    }


}
